<template>
  <van-cell-group>
    <van-card
        :thumb="userInfo.avatarUrl"
    />
    <van-cell title="编号" :value="userInfo.id"/>
    <van-cell title="名称" is-link :value="userInfo.username"/>
    <van-cell title="账号" is-link :value="userInfo.userAccount" @click="toEdit(userInfo)"/>
    <!--    <van-cell title="头像" :value="userInfo.avatarUrl"/>-->
    <van-cell title="性别" :value="userInfo.gender"/>
    <van-cell title="手机号" :value="userInfo.phone"/>
    <van-cell title="邮箱" :value="userInfo.email"/>
    <van-cell title="状态" :value="userInfo.userStatus"/>
    <van-cell title="角色" :value="userInfo.userRole"/>
    <van-cell title="用户代码" :value="userInfo.planetCode"/>
<!--    <van-cell title="标签" :value="userInfo.tags.join(', ')"/>-->
    <van-cell title="注册时间" :value="formattedCreateTime(userInfo.createTime)"/>
  </van-cell-group>

</template>

<script setup lang="ts">

import {onMounted,ref} from "vue";
import {userType} from "../model/userInfo";
import {useRouter} from "vue-router";
import myAxios from "../plugins/myAxios.ts";
import {getAuthToken, setAuthToken} from "../utils/auth.js";
import {Toast} from 'vant';

const router = useRouter()

const userInfo = ref({
  id: '1',
  username: '',
  userAccount: '',
  avatarUrl: '',
  gender: '',
  phone: '',
  email: '',
  userStatus: '',
  userRole: '',
  planetCode: '',
  tags: ['Java', 'Vue'],
  createTime: new Date(),
})

const formattedCreateTime = (value: Date) => {
  return new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }).format(value);
}

onMounted(async () =>{

  myAxios.post('/user/getByAccount', {
    userAccount: getAuthToken(),
  }).then(function (response) {
    let data = response.data;
    console.log('用户登录信息' + JSON.stringify(data));
    if (data.data != null && data.code == 200) {
      userInfo.value.id = data.data.id
      userInfo.value.username = data.data.userName
      userInfo.value.userAccount = data.data.userAccount
      userInfo.value.gender = data.data.gender
      userInfo.value.avatarUrl = data.data.avatarUrl
      userInfo.value.phone = data.data.phone
      userInfo.value.email = data.data.email
      userInfo.value.createTime = new Date(data.data.createTime)
    } else {
      return Toast.fail(data.message);
    }
  }).catch(function () {
    return Toast.fail('臣妾做不到~！');
  });
})
const toEdit = (value: userType) => {
  console.log(value)
  console.log(value.email)
  router.push({
    path: '/user/edit',
    query: {
      id: value.id,
      username: value.username,
      userAccount: value.userAccount,
      avatarUrl: value.avatarUrl,
      gender: value.gender,
      phone: value.phone,
      email: value.email,
    }
  })
}

</script>
<style scoped>

</style>